/*
* TrackControllerModule.java
* Track Controller Module, controls all track controllers
* Author: Nikolas Parshook
* Date Created: 2012-12-13
* Date Last Updated: 2012-12-13
*/

import javax.swing.*;

import java.awt.EventQueue;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
* Track Controller Module, controls all track controllers
* @author Nikolas Parshook
*/
public class TrackControllerModule
{
	CTCGUI ctc;
	TrackModel tm;
	TrackControllerPanel panel;
	TrackController [] tcs;
	int currentController;
	public Date displayDate = new Date();
	public SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	  
	
	/**
	* Constructor
	* @param ctc CTCGUI that controls the modules
	*/
	public TrackControllerModule(CTCGUI ctc)
	{
		if(ctc != null)
			this.ctc = ctc;
		//tm = new TrackModel();
		try {
			TrackModel.build();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tcs = new TrackController[TrackModel.redControllers.length + TrackModel.greenControllers.length];
		int i;
		for(i = 0; i < TrackModel.redControllers.length; i++)
		{
			tcs[i] = new TrackController(TrackModel.redControllers[i], i);
		}
		for(int j = 0; j < TrackModel.greenControllers.length; i++, j++)
		{
			tcs[i] = new TrackController(TrackModel.greenControllers[j], i);
		}
		System.out.println(tcs.length);
		currentController = 0;
		panel = new TrackControllerPanel(this);
		panel.initialize();
	}
	
	public CTCBundle receiveBundle(){return null;}
	
	/**
	* Gets the bundle from the ctc
	* @param CTCBundle
	*/
	public void receiveBundle(CTCBundle b) 
	{
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		TKBundle bun = new TKBundle();
		bun.speed = b.speed;
		panel.status = "Track Controller: CTC bundle received.";
		displayDate = new Date();
        
		//System.out.println(panel.status);
		panel.lblBundle.setText(panel.status);
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.run();
		sendBundle(bun);
	}
	
	/**
	* Sends bundle to TrackModel
	* @param TKBundle
	*/
	public void sendBundle(TKBundle b) 
	{
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		panel.status = "Track Controller: Track Model bundle sent.";
		System.out.println(panel.status);
		panel.lblBundle.setText(panel.status);
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			TrackModel.receive(b);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	* Sends bundle to ctc
	* @return CTCBundle
	*/
	public CTCBundle sendBundle() {return this.receiveBundle();}
	
	
	/**
	* gets the display panel for the module
	* @return TrackControllerPanel
	*/
	public TrackControllerPanel getPanel()
	{
		return panel;
	}
	
	
	/**
	* Runs the track controllers plcs
	*/
	private void run()
	{
		for(int i = 0; i < tcs.length; i++)
			tcs[i].run();
	}
	
	public static void main(String [] args)
	{
		final TrackControllerModule tcm = new TrackControllerModule(null);
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					//tcm.panel.initialize();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		tcm.receiveBundle(new CTCBundle());
	}
	
}
